package han.cirno.corrupt.modules.classes

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.widget.SeekBar
import android.widget.SeekBar.OnSeekBarChangeListener
import androidx.cardview.widget.CardView
import androidx.recyclerview.widget.GridLayoutManager
import han.cirno.corrupt.data.instances.UserClassesData
import han.cirno.corrupt.databinding.DialogClassDetailBinding
import han.cirno.corrupt.dialog.BottomPopDialog
import han.cirno.corrupt.util.ViewUtil
import han.cirno.corrupt.widgets.WeekSelectAdapter
import kotlin.math.min

@SuppressLint("SetTextI18n")
class ClassDetailDialog(context: Context,classEntry: UserClassesData.ClassEntry,cardView: CardView) :BottomPopDialog<DialogClassDetailBinding>(context, DialogClassDetailBinding.inflate(LayoutInflater.from(context))) {
    val adapter:WeekSelectAdapter = WeekSelectAdapter(context,classEntry.weeks,classEntry.user)

    init {
        val recyclerView = viewBinding.dialogClassDetailWeekRecyclerview
        recyclerView.adapter=adapter
        recyclerView.layoutManager=GridLayoutManager(context,5)
        viewBinding.dialogClassDetailClassName.text=classEntry.name
        viewBinding.dialogClassDetailLocation.text=classEntry.place
        viewBinding.dialogClassDetailTeacher.text=classEntry.teachers
        setupSeekbar(classEntry.time.from,classEntry.time.to-classEntry.time.from)
        viewBinding.dialogClassDetailTimeOffsetSeekbar.setOnSeekBarChangeListener(object :OnSeekBarChangeListener{
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                if(!fromUser)return
                val maxV = 11 - progress
                viewBinding.dialogClassDetailTimeLengthSeekbar.max = maxV
                viewBinding.dialogClassDetailTimeLengthSeekbar.progress=
                    min(viewBinding.dialogClassDetailTimeOffsetSeekbar.progress,maxV)
                    .also { viewBinding.dialogClassDetailTimeLengthText.text="${it+1}" }
                viewBinding.dialogClassDetailTimeOffsetText.text="${progress+1}"
            }
            override fun onStartTrackingTouch(seekBar: SeekBar?) {}
            override fun onStopTrackingTouch(seekBar: SeekBar?) {}
        })
        viewBinding.dialogClassDetailTimeLengthSeekbar.setOnSeekBarChangeListener(object :OnSeekBarChangeListener{
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                viewBinding.dialogClassDetailTimeLengthText.text="${progress+1}"
            }
            override fun onStartTrackingTouch(seekBar: SeekBar?) {}
            override fun onStopTrackingTouch(seekBar: SeekBar?) {}
        })
        ViewUtil.batchEnable(
            viewBinding.dialogClassDetailTimeOffsetSeekbar,
            viewBinding.dialogClassDetailTimeLengthSeekbar,
            supplier = {classEntry.user})
    }

    private fun setupSeekbar(timeOffset:Int,timeLength:Int) {
        viewBinding.dialogClassDetailTimeOffsetSeekbar.progress = timeOffset
        viewBinding.dialogClassDetailTimeLengthSeekbar.max = 11 - timeOffset
        viewBinding.dialogClassDetailTimeLengthSeekbar.progress = timeLength
        viewBinding.dialogClassDetailTimeOffsetText.text="${timeOffset+1}"
        viewBinding.dialogClassDetailTimeLengthText.text="${timeLength+1}"
    }
}